package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.entity.Classes;
import org.example.xsda.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级控制器
 */
@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @GetMapping("/page")
    @SaCheckRole("admin")
    public Result<PageResult<Classes>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long deptId
    ) {
        PageResult<Classes> pageResult = classService.page(current, size, keyword, deptId);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result<List<Classes>> list(@RequestParam(required = false) Long deptId) {
        List<Classes> list = classService.list(deptId);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Classes> getById(@PathVariable Long id) {
        Classes classes = classService.getById(id);
        return Result.success(classes);
    }

    @SaCheckRole("admin")
    @PostMapping
    public Result<Void> add(@Validated @RequestBody Classes classes) {
        classService.add(classes);
        return Result.success();
    }

    @SaCheckRole("admin")
    @PutMapping
    public Result<Void> update(@Validated @RequestBody Classes classes) {
        classService.update(classes);
        return Result.success();
    }

    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        classService.delete(id);
        return Result.success();
    }
}

